package com.example.demo.service;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;

import com.example.demo.config.PortSerivce;
import com.example.demo.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AsyncService {
    public static final String COMPONENT_HOST_KEY = "COMPONENT_HOST";

    public static final String COMPONENT_PORT_KEY = "COMPONENT_PORT";

    public static final String SELF_INVOKE_SWITCH_KEY = "SELF_INVOKE_SWITCH";

    public static final String COMPONENT_INVOKE_SWITCH_KEY = "COMPONENT_INVOKE_SWITCH";

    @Autowired
    private PortSerivce portSerivce;

    @PostConstruct
    public void init_async() {
        String myselfAddr = "localhost:" + portSerivce.getPort();
        String componentAddr = "";
        Map<String, String> envMap = System.getenv();
        if (envMap.containsKey("COMPONENT_HOST") && !((String) envMap.get("COMPONENT_HOST")).equals(""))
            if (envMap.containsKey("COMPONENT_PORT") && !((String) envMap.get("COMPONENT_PORT")).equals("")) {
                componentAddr = (String) envMap.get("COMPONENT_HOST") + ":" + (String) envMap.get("COMPONENT_PORT");
                System.out.println("component service address get from env.");
                System.out.println("component service address: " + componentAddr);
            } else {
                componentAddr = envMap.get("COMPONENT_HOST");
                System.out.println("component service host get from env.");
                System.out.println("component service address: " + componentAddr);
            }
        if (!envMap.containsKey("SELF_INVOKE_SWITCH") || !((String) envMap.get("SELF_INVOKE_SWITCH")).toUpperCase().equals("false".toUpperCase()))
            startSchedulerSelf(myselfAddr);
        if (!envMap.containsKey("COMPONENT_INVOKE_SWITCH") || !((String) envMap.get("COMPONENT_INVOKE_SWITCH")).toUpperCase().equals("false".toUpperCase()))
            if (componentAddr != null && !componentAddr.equals("")) {
                startSchedulerComponent(myselfAddr, componentAddr);
            } else {
                startSchedulerComponent(myselfAddr, myselfAddr);
            }
        System.out.println("start all schedulers.");
    }

    private void startSchedulerSelf(String address) {
        boolean flag_switch = true;
        long initDelay = 10L;
        long period = 20L;
        String myselfUrl = "http://" + address + portSerivce.getPath() + "/queryUser/10";
        System.out.println("start to invoke. " + myselfUrl);
        scheduleExecute(flag_switch, 10L, 20L, myselfUrl, "");
        boolean exp_db_flag_switch = true;
        long exp_db_initDelay = 10L;
        long exp_db_period = 120L;
        String exp_db_myselfUrl = "http://" + address + portSerivce.getPath() + "/queryNotExistDB/11";
        System.out.println("start to invoke. " + exp_db_myselfUrl);
        scheduleExecute(exp_db_flag_switch, 10L, 120L, exp_db_myselfUrl, "");
        boolean exp_self_invoke_switch = true;
        long exp_initDelay = 10L;
        long exp_period = 120L;
        String exp_myselfUrl = "http://" + address + portSerivce.getPath() + "/queryException/12";
        System.out.println("start to invoke. " + exp_myselfUrl);
        scheduleExecute(exp_self_invoke_switch, 10L, 120L, exp_myselfUrl, "");
        boolean exp_randRT_invoke_switch = true;
        long exp_randRT_initDelay = 10L;
        long exp_randRT_period = 60L;
        String exp_randRTUrl = "http://" + address + portSerivce.getPath() + "/randomRT/12";
        System.out.println("start to invoke randRT. " + exp_randRTUrl);
        scheduleExecute(exp_randRT_invoke_switch, 10L, 60L, exp_randRTUrl, "");
        System.out.println("start schedule. address: " + address);
    }

    private void startSchedulerComponent(String selfAddr, String componentAddr) {
        String selfComponentInvokeUrl = "http://" + selfAddr + portSerivce.getPath() + "/invokeConponent";
        boolean flag_switch = true;
        long initDelay = 10L;
        long period = 20L;
        String succUrl = componentAddr + portSerivce.getPath() + "/queryUser/10";
        System.out.println("start to invoke component. " + succUrl + " mysqlUrl: " + selfAddr);
        scheduleExecute(flag_switch, 10L, 20L, selfComponentInvokeUrl, "param=" + succUrl);
        boolean exp_db_flag_switch = true;
        long exp_db_initDelay = 10L;
        long exp_db_period = 120L;
        String exp_db_Url = componentAddr + portSerivce.getPath() + "/queryNotExistDB/11";
        System.out.println("start to invoke. " + exp_db_Url);
        scheduleExecute(exp_db_flag_switch, 10L, 120L, selfComponentInvokeUrl, "param=" + exp_db_Url);
        boolean exp_self_invoke_switch = true;
        long exp_initDelay = 10L;
        long exp_period = 120L;
        String exp_Url = componentAddr + portSerivce.getPath() + "/queryException/12";
        System.out.println("start to invoke. " + exp_Url);
        scheduleExecute(exp_self_invoke_switch, 10L, 120L, selfComponentInvokeUrl, "param=" + exp_Url);
        boolean exp_randRT_invoke_switch = true;
        long exp_randRT_initDelay = 10L;
        long exp_randRT_period = 60L;
        String exp_randRTUrl = componentAddr + portSerivce.getPath() + "/randomRT/12";
        System.out.println("start to invoke randRT. " + exp_randRTUrl);
        scheduleExecute(exp_randRT_invoke_switch, 10L, 60L, selfComponentInvokeUrl, "param=" + exp_randRTUrl);
        System.out.println("start schedule component. self address: " + selfAddr + " componentAddr: " + componentAddr);
    }

    private void scheduleExecute(boolean flag_switch, long initDelay_s, long period_s, String url, String param) {
        if (flag_switch) {
            ScheduledExecutorService ScheduledExecutorService = Executors.newScheduledThreadPool(1);
            ScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    String ret = null;

                    try {
                        ret = Util.sendGet(url, param);
                    } catch (IOException var3) {
                        var3.printStackTrace();
                    }

                    System.out.println(ret);
                }
            }, initDelay_s, period_s, TimeUnit.SECONDS);
            System.out.println("[start scheduler] url: " + url + " param: " + param + " initDelay_s: " + initDelay_s + " period_s: " + period_s);
        }
    }
}
